import { configureStore, createSlice, PayloadAction } from '@reduxjs/toolkit';
// 定义状态类型
interface CounterState {
  value: number;
}

// 初始化状态
const initialState: CounterState = { value: 0 };

// 创建 Slice（包含 reducer 和 actions）
const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    },
  },
});

// 导出 Action 和 Reducer
export const { increment, incrementByAmount } = counterSlice.actions;
export const store = configureStore({
  reducer: {
    counter: counterSlice.reducer,
  },
});

// 定义 RootState 和 AppDispatch 类型
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
